//获取首页热榜
var express = require("express");
var router = express.Router();

let article = require("../model/article")
let resource = require("../model/resource")
let problem = require("../model/problem")

router.post("/api/v1/getHot", async (req, res) => {
  console.log(req.body);
  let skip = req.body.skip
  let limit = req.body.limit


  let articleData = await article.find({
    audit: true
  }).sort({
    reading: -1
  }).limit(20)

  let resourceData = await resource.find({
    audit: true
  }).sort({
    reading: -1
  }).limit(20)

  let problemData = await problem.find({
    audit: true
  }).sort({
    reading: -1
  }).limit(20)

  let searchData = articleData.concat(resourceData, problemData)

  searchData.sort((a, b) => {
    return Date.parse(b.reading) - Date.parse(a.reading)
  })
  let nmu = searchData.length
  let sendData = searchData.splice(skip, limit)

  res.json({
    code: 200,
    data: sendData,
    count: nmu
  });


})

// 导出模块（在 app.js 中引入）
module.exports = router;